//
//  InforTopScrollView.m
//  YiXiProject
//
//  Created by xingshuai on 2019/7/27.
//  Copyright © 2019 iMac. All rights reserved.
//

#import "InforTopScrollView.h"

@implementation InforTopScrollView

//自定义init方法/构造方法
- (id)initWithFrame:(CGRect)frame addImageArray:(NSMutableArray *)addImageArray addPlaceholderImg:(NSString *)img
{
    if (self = [super initWithFrame:frame])
    {
        if(SCREEN_HEIGHT > 736)
        {
            _heightPage = 44 + 20;
        }
        else
        {
            _heightPage = 20 + 20;
        }
        
        //获取self宽度
        _width = self.frame.size.width;
        
        //获取self高度
        _height = self.frame.size.height;
        
        _imgPlaceholder = img;
        //改变图片数组 1 2 3 4 5 6
        _dataArray = [NSMutableArray arrayWithArray:addImageArray];
        
        if(_dataArray.count == 0)
        {
            
        }
        else
        {
            //在数组的最后一位添加上第一张图片 1 2 3 4 5 6 1
            [_dataArray addObject:addImageArray[0]];
            
            //在第一个位置插入图片6 | 6 1 2 3 4 5 6 1
            [_dataArray insertObject:[addImageArray lastObject] atIndex:0];
        }
        
        // 去掉y头部空白
        if (@available(iOS 11.0, *)) {
            self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        //将scrollView添加到self上
        [self addSubview:self.scrollView];
        
        //添加page
        [self addSubview:self.pageControl];
    }
    return self;
}

//初始化scrollView
- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        
        //滚动范围
        _scrollView.contentSize = CGSizeMake(_width * _dataArray.count, _height);
        
        //起始页
        _scrollView.contentOffset = CGPointMake(_width, 0);
        
        //添加分页效果
        _scrollView.pagingEnabled = YES;
        
        //禁止弹簧效果
        _scrollView.bounces = NO;
        
        _scrollView.delegate = self;
        
        //禁止水平滚动
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        if(_dataArray.count == 0)
        {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_width, 0, _width, _height)];
            
//            imageView.image = [UIImage imageNamed:@"image_1.jpg"];
            [imageView sd_setImageWithURL:[NSURL URLWithString:_imgPlaceholder] placeholderImage:nil];
            
            //与用户交互
            imageView.userInteractionEnabled = YES;
            
            [_scrollView addSubview:imageView];
        }
        else
        {
            for (int i = 0; i < _dataArray.count; i++)
            {
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_width * i, 0, _width, _height)];
                
                [imageView sd_setImageWithURL:[NSURL URLWithString:_dataArray[i]] placeholderImage:nil];
                
                //与用户交互
                imageView.userInteractionEnabled = YES;
                
                [_scrollView addSubview:imageView];
            }
        }
    }
    
    return _scrollView;
}

//初始化pageControl
- (PageControl *)pageControl
{
    if (!_pageControl)
    {
        _pageControl = [[PageControl alloc]initWithFrame:CGRectMake(5, _heightPage, self.bounds.size.width-10, 10)];
        _pageControl.numberOfPages = _dataArray.count-2;
        _pageControl.currentPage = 0;
        [self addSubview:_pageControl];
        _pageControl.userInteractionEnabled = NO;
    }
    return _pageControl;
}

//代理方法  已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取滑动结束的偏移量
    CGPoint currentOffSet = scrollView.contentOffset;
    
    //如果是在最后一页的话 , 让scrollView滚到第一页
    if (currentOffSet.x == (_dataArray.count - 1)* _width)
    {
        _scrollView.contentOffset = CGPointMake(_width, 0);
    }
    
    //如果是第一页的时候 , 偏移量为0
    if (currentOffSet.x == 0)
    {
        _scrollView.contentOffset = CGPointMake((_dataArray.count - 2) * _width, 0);
    }
    
    //获取最新偏移量
    CGPoint newOffSet = _scrollView.contentOffset;
    
    //获取偏移了多少个宽度
    NSInteger tmpPage = newOffSet.x / _width;
    
    //设置当前pageControl的当前点
    _pageControl.currentPage = tmpPage - 1;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
